//
//  MHHelperViewModel.m
//  MiaoCircle
//
//  Created by 许正荣 on 2018/6/25.
//  Copyright © 2018年 acgera. All rights reserved.
//

#import "MHHelperViewModel.h"
#import "MHCommonArrowItemViewModel.h"
#import "MHHelperModel.h"

@interface MHHelperViewModel()

@property (nonatomic, readwrite, copy) NSArray *repeaters;

@end

@implementation MHHelperViewModel
-(void)initialize
{
    [super initialize];
    self.title = @"帮助与反馈";
    self.shouldPullUpToLoadMore = YES;
    /// 没有数据时，隐藏底部刷新控件
    self.shouldEndRefreshingWithNoMoreData = NO;
    
    @weakify(self);
    RAC(self, repeaters) = self.requestRemoteDataCommand.executionSignals.switchToLatest;
    /// 数据源
    RAC(self,dataSource) = [RACObserve(self, repeaters) map:^(NSArray * repeaters) {
        @strongify(self)
        
        MHCommonGroupViewModel *group1 = [MHCommonGroupViewModel groupViewModel];
        group1.itemViewModels = [self dataSourceWithRepeaters:repeaters];
        return @[group1];
    }];
    
    self.redirectToFeedBack = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {

        MHFeedBackViewModel *feedBack = [[MHFeedBackViewModel alloc] initWithServices:self.services params:nil];
        [self.services pushViewModel:feedBack animated:YES];
        return  [RACSignal empty];
    }];
    
    
}

-(instancetype)initWithServices:(id<MHViewModelServices>)services params:(NSDictionary *)params
{
    if (self = [super initWithServices:services params:params]) {
//        self.info = params[MHViewModelUtilKey];
    }
    return self;
}


- (RACSignal *)requestRemoteDataSignalWithPage:(NSUInteger)page{
    
    NSArray * (^mapRepeaters)(NSArray *) = ^(NSArray *repeaters) {
        if (page == 1) {
            /// 下拉刷新
        } else {
            /// 上拉加载
            repeaters = @[(self.repeaters ?: @[]).rac_sequence, repeaters.rac_sequence].rac_sequence.flatten.array;
        }
        return repeaters;
    };
    
    return [[[self.services.client getHelpTipsList] mh_parsedResults] map:mapRepeaters];
}

#pragma mark - 辅助方法
- (NSArray *)dataSourceWithRepeaters:(NSArray *)repeaters {
    if (MHObjectIsNil(repeaters) || repeaters.count == 0) return @[];
    NSMutableArray *viewModels = [[repeaters.rac_sequence map:^(MHHelperModel *repeater) {
          MHCommonArrowItemViewModel *viewModel  = [MHCommonArrowItemViewModel itemViewModelWithTitle:repeater.contentTxt];
        return viewModel;
    }].array mutableCopy];
//    [viewModels appendObject:allViewModel];
    return viewModels ?: @[] ;
}

@end
